/**
 * 
 */
package com.revenco.bi.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.QueryResult;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.revenco.bi.dao.TnOthtableMaintableRelaDao;
import com.revenco.bi.dao.TnPublicDemensionValueSelDao;
import com.revenco.bi.model.TnOthtableMaintableRela;
import com.revenco.bi.model.TnPublicDemensionValueSel;
import com.revenco.bi.model.userbean.ConditonVal;
import com.revenco.bi.model.userbean.ConditonValList;
import com.revenco.bi.service.TnOthtableMaintableRelaService;

/**
 * @author LingJun
 * @version $Revision:1.0.0, 2014年1月18日 下午12:08:53
 */
@Service
public class TnOthtableMaintableRelaServiceImpl implements
		TnOthtableMaintableRelaService {
	@Autowired
	private TnOthtableMaintableRelaDao tnOthtableMaintableRelaDao;
	@Autowired
	private TnPublicDemensionValueSelDao tnPublicDemensionValueSelDao;

	public List<TnOthtableMaintableRela> getTnOthtableMaintableRelaByColumnId(
			String columnId) {
		return tnOthtableMaintableRelaDao
				.getTnOthtableMaintableRelaByColumnId(columnId);
	}

	/**
	 * 获取维度值
	 */
	public Map<String, String> getDemensionDataByColumnId(String columnId) {
		List<TnOthtableMaintableRela> list = tnOthtableMaintableRelaDao
				.getTnOthtableMaintableRelaByColumnId(columnId);
		Map<String, String> map = new HashMap<String, String>();
		if (list != null && list.size() > 0) {
			String pvId = list.get(0).getPvId();
			List<TnPublicDemensionValueSel> listT = null;
			listT = tnPublicDemensionValueSelDao
					.getTnPublicDemensionValueSelByPvId(pvId);
			if (listT != null && listT.size() > 0) {
				TnPublicDemensionValueSel tempObj = listT.get(0);
				map = tnPublicDemensionValueSelDao.getDemensionValues(
						tempObj.getTextColName(), tempObj.getValueColName(),
						tempObj.getConfigUrl());
			}
		}
		return map;
	}

	/**
	 * 获取维度值
	 */
	public ConditonValList getDemensionDataByColumnIdQr(String columnId, Pager pager) {
		List<TnOthtableMaintableRela> list = tnOthtableMaintableRelaDao
				.getTnOthtableMaintableRelaByColumnId(columnId);
		ConditonValList conditonValList = new ConditonValList();
		QueryResult queryResult = null;
		ConditonVal[] cvArray = null;
		TnPublicDemensionValueSel tempObj = null;
		if (list != null && list.size() > 0) {
			String pvId = list.get(0).getPvId();
			List<TnPublicDemensionValueSel> listT = null;
			listT = tnPublicDemensionValueSelDao
					.getTnPublicDemensionValueSelByPvId(pvId);
			if (listT != null && listT.size() > 0) {
				tempObj = listT.get(0);
				queryResult = tnPublicDemensionValueSelDao
						.getDemensionValuesQr(tempObj.getTextColName(),
								tempObj.getValueColName(),
								tempObj.getConfigUrl(), pager);
			}
		}
		// 默认值重新组装
		if (queryResult != null && tempObj != null) {
			@SuppressWarnings("unchecked")
			List<Record> recordList = (List<Record>) queryResult.getList();
			int recordSize = recordList.size();
			cvArray = new ConditonVal[recordSize];
			for (int i = 0; i < recordSize; i++) {
				ConditonVal cv = new ConditonVal();
				Record r = recordList.get(i);
				cv.setName(r.get(tempObj.getTextColName()).toString());
				cv.setValue(r.get(tempObj.getTextColName()).toString());
				cvArray[i] = cv;
			}
			conditonValList.setConditonVal(cvArray);
			conditonValList.setCount(queryResult.getPager().getRecordCount());
		}
		return conditonValList;
	}

	/**
	 * 获取维表值配置信息
	 */
	public List<TnPublicDemensionValueSel> getTnPublicDemensionValueSelByPvId(
			String columnId) {
		List<TnOthtableMaintableRela> list = tnOthtableMaintableRelaDao
				.getTnOthtableMaintableRelaByColumnId(columnId);
		List<TnPublicDemensionValueSel> listT = null;

		if (list != null && list.size() > 0) {
			String pvId = list.get(0).getPvId();
			listT = tnPublicDemensionValueSelDao
					.getTnPublicDemensionValueSelByPvId(pvId);
		}
		return listT;
	}
}
